package com.domi.route.config;

import com.domi.route.NacosRouteDefinitionRepository;
import com.domi.route.RefreshRouteLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DynamicRouteConfig {

    private String dataId = "gateway-router.yaml";

    private String group = "DEFAULT_GROUP";

    @Value("${spring.cloud.nacos.config.server-addr}")
    private String serverAddr;

    @Value("${spring.cloud.nacos.config.namespace}")
    private String namespace;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Bean
    public RouteDefinitionRepository routeDefinitionRepository() {
        return new NacosRouteDefinitionRepository(applicationEventPublisher, serverAddr, namespace, dataId, group);
    }

    @Bean
    public RouteLocator routeLocator() {
        return new RefreshRouteLocator();
    }

}